package com.xxl.shoppingapp.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.xxl.baselib.app.AppManager;
import com.xxl.baselib.baseui.activity.BaseWyActivity;
import com.xxl.baselib.baseui.fragment.BaseWyFragment;
import com.xxl.baselib.baseui.widget.TitleBar;

import butterknife.ButterKnife;


/**
 * Created by weiyi on 15/10/20.
 */
public abstract class BaseFragment extends BaseWyFragment {


    @Override
    final protected void initWyView(View view) {
        super.initWyView(view);
        ButterKnife.bind(this, view);
        initView(view);
    }

    protected void initView(View view) {
    }

    protected BaseWyActivity activity() {
        return getBaseActivity();
    }

    protected void setTitle(String title) {
        TitleBar titleBar = getBaseActivity().getTitleBar();
        if (titleBar != null) {
            titleBar.setTitle(title);
        }
    }

    /**
     * 跳转Fragment,通过Activity跳转
     *
     * @param fragment
     */
    protected void startFragment(Fragment fragment) {
        CommonActivity.showFromFragment(this, fragment);
    }

    protected void startFragmentWithData(Fragment fragment, Bundle bundle) {
        fragment.setArguments(bundle);
        CommonActivity.showFromFragment(this, fragment);
    }

    public static void startActivity(Fragment from, Class clas, Bundle bundle) {
        Intent intent = new Intent(from.getActivity(), clas);
        intent.putExtras(bundle);
        from.getActivity().startActivity(intent);
    }

    public static void startActivityForResult(Fragment from, Class clas, Bundle bundle , int requestCode) {
        Intent intent = new Intent(from.getActivity(), clas);
        intent.putExtras(bundle);
        from.getActivity().startActivityForResult(intent,requestCode);
    }

    protected void startFragmentForResult(Fragment fragment, int requestCode) {
        CommonActivity.showFromFragmentFroResult(this, fragment, requestCode);
    }

    public void finishActivity() {
        AppManager.getAppManager().finishActivity(getBaseActivity());
    }
}
